<template>
  <div class="mod-config">
    <table-view ref="view"
                :searchAttrs="searchAttrs"
                :primaryKey="primaryKey"
                :listApiName="listApiName"
                :deleteApiName="deleteApiName"
                :saveAuthName="saveAuthName"
                :deleteAuthName="deleteAuthName"
                :showAttrs="showAttrs"
                :exportApiName="exportApiName"
                :printFunc='printFunc'
                :multiAdd='true'
    >
    </table-view>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <client-info-add-or-update v-if="clientInfoVisible" ref="clientInfo"></client-info-add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './productleavestorage-add-or-update-1.vue'
import tableView from '@/components/tableView'
import basicMixins from '@/utils/commonViewMixins'
import ClientInfoAddOrUpdate from '@/views/modules/client/clientinfo-add-or-update.vue'
import {exportHandle, isAuth} from '@/utils'
import {ProductLeavePrintModel} from '@/components/printModel/productLeavePrintModel'

export default {
  mixins: [basicMixins],
  data() {
    return {
      sAuth: isAuth,
      clientInfoVisible: false,
      searchAttrs: {
        productId: {
          type: 'autoComplete',
          placeholder: '请选择产品',
          querySearchApiName: 'productApi/getProductInfoList'
        },
        clientId: {
          type: 'autoComplete',
          placeholder: '请选择客户',
          querySearchApiName: 'clientApi/getClientList'
        },
        dateTimeRange: {
          type: 'date',
          dateType: 'daterange',
          clearable: true
        }
      },
      primaryKey: 'id',
      listApiName: 'productApi/listProductLeaveStorage',
      deleteApiName: 'productApi/deleteProductLeaveStorage',
      downloadApiName: 'productApi/getProductReconciliationExport',
      saveAuthName: 'product:productleavestorage:save',
      deleteAuthName: 'product:productleavestorage:delete',
      downloadHandleName: 'finance:financepay:list',
      exportApiName: 'productApi/getProductLeaveStorageExport',
      showAttrs: {
        clientName: {
          label: '客户名称',
          type: 'linkLabel',
          routerName: 'client-clientinfo',
          searchKey: 'clientName'
        },
        productNo: {
          label: '产品编号',
          type: 'linkLabel',
          routerName: 'product-product-info-all',
          searchKey: 'productNo',
          width: 100
        },
        productWeight: {label: '克数'},
        boxSupplyWay: {
          label: '包装规格',
          type: 'radio',
          items: [
            {value: 0, text: '纸箱', tagType: 'success'},
            {value: 1, text: '托盘'},
            {value: 2, text: '客供', tagType: 'danger'}
          ]
        },
        //  boxNo: {label: '纸箱编号'},
        boxZhiNumber: {label: '箱只'},
        boxNumber: {label: '箱数'},
        // trayInfoNo: {label: '托盘编号'},
        assortBoxNumber: {label: '配箱数量'},
        assortBoxZhiNumber: {label: '配箱只数'},
        tailBoxNumber: {label: '尾箱数量'},
        tailBoxZhiNumber: {label: '尾箱只数'},
        // trayNumber: {label: '托盘数量'},
        weightCount: {label: '总克数(g)'},
        tonnageWeightCount: {label: '总吨数(t)'},
        //saleNumber: {label: '销售数量'},
        realSaleNumber: {label: '销售数量'},
        orderDetailNo: {
          label: '订单编号',
          width: 150
        },
        outTime: {label: '出库时间', type: 'date', width: 100},
        tickerNo: {label: '出库单号', width: 120},
        remark: {label: '备注'},
        status: {
          label: '状态',
          type: 'radio',
          items: [
            {value: 0, text: '禁用', tagType: 'danger'},
            {value: 1, text: '正常'}
          ]
        }
      }
    }
  },
  methods: {
    printFunc(rows) {
      this.$vBus.$emit('SHOW_PRINT_DIALOG', (win) => {
        const tmpDocument = this.R.prop('document', win)
        const content = tmpDocument.querySelector('#content')

        if (this.R.isNil(tmpDocument) || this.R.isNil(content)) {
          return
        }
        content.innerHTML = ''

        const addTd = (tr, str) => {
          const td = tmpDocument.createElement('td')

          td.innerText = str
          tr.appendChild(td)
        }
        const {outTime} = rows[0]
        const {tickerNo} = rows[0]

        tmpDocument.querySelector('#time').innerHTML = outTime.toString().substr(0,11)
        tmpDocument.querySelector('#tickerNo').innerHTML = tickerNo.toString()

        rows.forEach(row => {
          const {clientName, productNo, productWeight, boxSupplyWay, boxZhiNumber, trayEachZhiNumber, trayLayerNumber, boxNumber, trayNumber, weightCount, realSaleNumber,remark} = row
          const boxSupplyWayName = boxSupplyWay === 0 ? '纸箱' : boxSupplyWay===1 ? '托盘': '自供'
          const tr = tmpDocument.createElement('tr')

          addTd(tr, clientName)
          addTd(tr, productNo)
          addTd(tr, boxSupplyWayName)
          if (boxSupplyWay === 0) {
            addTd(tr, boxZhiNumber)
            addTd(tr, boxNumber)
          }
          else if (boxSupplyWay === 1) {
            addTd(tr, trayEachZhiNumber * trayLayerNumber)
            addTd(tr, trayNumber)
          } else {
            addTd(tr, boxZhiNumber )
            addTd(tr, boxNumber)
          }
          addTd(tr, realSaleNumber)
          addTd(tr, productWeight)
          addTd(tr, weightCount)
          addTd(tr, remark)

          content.appendChild(tr)
        })
      }, true)
    },
    downloadHandle() {
      return exportHandle(this.downloadApiName)
    },
    addMulti() {
      this.$refs.editTableView.open('批量出库')
    },
    openClientInfoDialog(cb) {
      this.clientInfoVisible = true
      this.$nextTick(() => {
        if (!this.R.isNil(this.$refs.clientInfo)) {
          this.$refs.clientInfo.init(null, {status: 1}, cb)
        }
      })
    }
  },
  components: {
    tableView,
    AddOrUpdate,
    ClientInfoAddOrUpdate
  }
}
</script>
